package com.cjw.campus.system.controller;

import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.cjw.campus.core.result.JsonResult;
import com.cjw.campus.system.entity.ForegroundUserTable;
import com.cjw.campus.system.service.IForegroundUserTableService;
import com.cjw.campus.system.service.IStudentTableService;
import com.cjw.campus.system.vo.StudentTableVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2023-02-28
 */
@RestController
@RequestMapping("/system/studentTable")
@Tag(name = "学生管理接口")
public class StudentTableController {

    @Autowired
    private IStudentTableService iStudentTableService;

    @Autowired
    private IForegroundUserTableService iForegroundUserTableService;

    @GetMapping("/studentAttestation")
    @Operation(summary = "学生认证", security = { @SecurityRequirement(name = "Authorization")})
    public JsonResult studentAttestation (String matriculationYearName, String studentName, Integer studentNumber, @RequestHeader("Authorization") String token) {
        StudentTableVO list = iStudentTableService.listByStudentInfo(matriculationYearName, studentName, studentNumber);
        if (list == null) {
            return JsonResult.error();
        }

        // 获取token 赋值给jwt
        JWT jwt = JWTUtil.parseToken(token);
        //  解析token里的前台用户信息
        Object object = jwt.getPayload("foregroundUser");
        ForegroundUserTable foregroundUser = JSONUtil.toBean(JSONUtil.toJsonStr(object), ForegroundUserTable.class);
        // 设置用户类型， 0为学生
        foregroundUser.setUserType(0);
        foregroundUser.setTeacherId(0L);
        foregroundUser.setStudentId(list.getStudentId());
        foregroundUser.setNickName(list.getStudentName());
        iForegroundUserTableService.updateById(foregroundUser);
        return JsonResult.ok(list);
    }

}
